import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';

import { HttpWikiService } from './http-wiki.service';

@Component({
    selector: 'guide-http-wiki-smart',
    templateUrl: './http-wiki.component.html',
    providers: [HttpWikiService]
})
export class HttpWikiSmartComponent implements OnInit {
    title = '更聪明的维基百科Demo';
    fetches = '停止输入的时候获取';
    items: Observable<string[]>;
    private searchTermStream = new Subject<string>();

    constructor(private wikiService: HttpWikiService) { }

    search(term: string) { this.searchTermStream.next(term); }

    ngOnInit() {
        this.items = this.searchTermStream
            .debounceTime(300)
            .distinctUntilChanged()
            .switchMap((term: string) => this.wikiService.search(term));
    }
}
